package com.dangs.autotest.system;

import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.PowerManager;

import com.dangs.autotest.tool.L;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public abstract class CommonInterfaceImp implements CommonInterface {

    protected String TAG = this.getClass().getSimpleName();

    CommonInterfaceImp() {
        L.i(TAG, "API imp:" + TAG);
    }

    @Override
    public void reboot(Context context) {
        PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        powerManager.reboot(null);
    }

    public String getRamUsedRate(Context context) {
        String result;
        try {
            ActivityManager.MemoryInfo memInfo = new ActivityManager.MemoryInfo();
            ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
            am.getMemoryInfo(memInfo);
            boolean lowMem = memInfo.lowMemory;
            float totalMem = memInfo.totalMem;
            float availMem = memInfo.availMem;
            float mem_used = (totalMem - availMem) * 100 / totalMem;
            result = String.format("%.2f", mem_used) + "%";
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "none";
    }

    public String getRamInfo(Context context) {
        String result;
        try {
            ActivityManager.MemoryInfo memInfo = new ActivityManager.MemoryInfo();
            ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
            am.getMemoryInfo(memInfo);
            float totalMem = (float) (memInfo.totalMem / 1024 / 1024 / 1024);
            float availMem = (float) (memInfo.availMem / 1024 / 1024 / 1024);
            result = String.format("%.1f/%.1fG", totalMem - availMem, totalMem);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "none";
    }

    public String getRealTime() {
        DateFormat df = new SimpleDateFormat("HH:mm:ss");
        String realTime = df.format(new Date(System.currentTimeMillis()));
        return realTime;
    }

    public String readLine(String filePath) {
        String ret = "";
        try {
            BufferedReader reader = new BufferedReader(new FileReader(filePath));
            ret = reader.readLine();
        } catch (FileNotFoundException e) {
            L.w(TAG, "read fail:" + e);
        } catch (IOException e) {
            L.w(TAG, "read fail:" + e);
        }
        return ret;
    }

    public void write(String filePath, String str) {
        try {
            BufferedWriter writer = new BufferedWriter(new FileWriter(filePath, false));
            writer.newLine();
            writer.write(str);
            writer.flush();
            writer.close();
        } catch (Exception e) {
            L.w(TAG, "failed to write record:" + e.toString());
        }
    }

    public String getTopActivity(Context context) {
        String topActivityPackage = "";
        ActivityManager activityManager = (ActivityManager) (context
                .getSystemService(android.content.Context.ACTIVITY_SERVICE));
        List<ActivityManager.RunningTaskInfo> runningTaskInfos = activityManager
                .getRunningTasks(1);
        if (runningTaskInfos != null) {
            ComponentName f = runningTaskInfos.get(0).topActivity;
            topActivityPackage = f.getPackageName();
        }
        return topActivityPackage;
    }


    @Override
    public String getCpuLoadRate() {
        L.d(TAG, "no implements");
        return "";
    }

    public void execCMD(String cmd) {
        try {
            Runtime.getRuntime().exec(cmd);
        } catch (IOException e) {
            L.w(TAG, "run fail:" + e);
        }
    }

    public String getVersion(Context context, String name) {
        String ret = "";
        try {
            PackageInfo info = context.getPackageManager().getPackageInfo(name, 0);
            ret = info.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return ret;
    }
}
